﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Negocio;

namespace Presentacion
{
    /// <summary>
    /// Lógica de interacción para HistoriaClinica.xaml
    /// </summary>
    public partial class WClinicalHistory : UserControl
    {
        private ClinicalHistory clinicalHistory;
        private WDisease wDisease;
        private WInjury wInjury;
        bool insert;
        public WClinicalHistory(ClinicalHistory oneClinicalHistory)
        {
            InitializeComponent();
            this.clinicalHistory = oneClinicalHistory;
            wDisease = new WDisease();
            gridData.Children.Add(wDisease);
            lblData.Content = "Enfermedad";
            lvwDisease.ItemsSource = clinicalHistory.Diseases;
            lvwInjury.ItemsSource = clinicalHistory.Injuries;
            insert = true;
            lblName.Content = clinicalHistory.Player.CompleteName;
            lblBirthDate.Content = clinicalHistory.Player.BirthDate.ToShortDateString();
        }

        private void btnInjury_Click(object sender, RoutedEventArgs e)
        {
            wInjury = new WInjury();
            gridData.Children.Clear();
            gridData.Children.Add(wInjury);
            lblData.Content = "Lesión";
            insert = true;
        }

        private void btnDisease_Click(object sender, RoutedEventArgs e)
        {
            wDisease = new WDisease();
            gridData.Children.Clear();
            gridData.Children.Add(wDisease);
            lblData.Content = "Enfermedad";
            insert = true;
        }

        private void btnEnter_Click(object sender, RoutedEventArgs e)
        {
            if (lblData.Content.Equals("Lesión"))
            {
                if (insert)
                {
                    Injury injury = Club.getInstance().newInjury(wInjury.dpDate.SelectedDate.Value, wInjury.tbxDescription.Text, wInjury.cbxStatus.Text, wInjury.tbxName.Text, wInjury.cbxScore.Text, wInjury.cbxZone.Text, wInjury.tbxActivity.Text, wInjury.tbxCause.Text, wInjury.tbxTreatment.Text, wInjury.tbxComments.Text);
                    bool isAdded = clinicalHistory.addInjury(injury);
                    clinicalHistory.updated();
                    if (!isAdded)
                    {
                        //mensaje
                    }
                }
                else
                {
                    if (lvwInjury.SelectedIndex != -1)
                    {
                        Club.getInstance().updateInjury(wInjury.dpDate.SelectedDate.Value, wInjury.tbxDescription.Text, wInjury.cbxStatus.Text, wInjury.tbxName.Text, wInjury.cbxScore.Text, wInjury.cbxZone.Text, wInjury.tbxActivity.Text, wInjury.tbxCause.Text, wInjury.tbxTreatment.Text, wInjury.tbxComments.Text, (Injury)lvwInjury.SelectedItem);
                        lvwInjury.ItemsSource = null;
                        lvwInjury.ItemsSource = clinicalHistory.Injuries;
                    }
                }
            }
            else
            {
                if (insert)
                {
                    Disease disease = Club.getInstance().newDisease(wDisease.cbxSystem.Text, wDisease.tbxName.Text, wDisease.tbxDescription.Text, 0, wDisease.tbxComments.Text, wDisease.cbxBirth.IsChecked.Value, wDisease.dpDate.SelectedDate.Value, wDisease.cbxStatus.Text);
                    bool isAdded = clinicalHistory.addDisease(disease);
                    clinicalHistory.updated();
                    if (!isAdded)
                    {
                        //mensaje
                    }
                }
                else
                {
                    //update
                    if (lvwDisease.SelectedIndex != -1)
                    {
                        Club.getInstance().updateDisease(wDisease.cbxSystem.Text, wDisease.tbxName.Text, wDisease.tbxDescription.Text, 0, wDisease.tbxComments.Text, wDisease.cbxBirth.IsChecked.Value, wDisease.dpDate.SelectedDate.Value, wDisease.cbxStatus.Text, (Disease)lvwDisease.SelectedItem);
                        lvwDisease.ItemsSource = null;
                        lvwDisease.ItemsSource = clinicalHistory.Diseases;
                    }
                    //menbaje
                }
            }
           
        }

        private void lvwInjury_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lvwInjury.SelectedIndex != -1) 
            {

                btnInjury_Click(sender, e);
                Injury injury = (Injury)lvwInjury.SelectedItem;
                wInjury.tbxName.Text = injury.Name;
                wInjury.tbxActivity.Text = injury.ActivityWhenInjured;
                wInjury.tbxCause.Text = injury.Cause;
                wInjury.tbxComments.Text = injury.Comments;
                wInjury.tbxDescription.Text = injury.Description;
                wInjury.tbxTreatment.Text = injury.TreatmentType;
                wInjury.cbxZone.Text = injury.AffectedArea;
                wInjury.cbxScore.Text = injury.Severity;
                wInjury.dpDate.SelectedDate = injury.Date;
                wInjury.cbxStatus.Text = injury.Status;
                insert = false;

            }
        }

        private void lvwDisease_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lvwDisease.SelectedIndex != -1)
            {
                btnDisease_Click(sender, e);
                Disease disease = (Disease)lvwDisease.SelectedItem;
                wDisease.tbxName.Text = disease.Name;                
                wDisease.tbxComments.Text = disease.Comment;
                wDisease.tbxDescription.Text = disease.Description;                
                wDisease.cbxBirth.IsChecked = disease.OfBirth;
                wDisease.cbxSystem.Text = disease.AffectedSystem;
                wDisease.dpDate.SelectedDate = disease.Date;
                wDisease.cbxStatus.Text = disease.Status;
                insert = false;
                //llenar datos de enfermedad.
            }
        }
    }
}
